package view
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.events.Event;

	public class AnimationObj extends Bitmap
	{
		private var timer:Timer = new Timer(100);
		
		private var currentImgList:Vector.<BitmapData> = new Vector.<BitmapData>;
		private var currentIndex:int = 0;
		private var _frames:Dictionary = new Dictionary();
		
		public function AnimationObj()
		{
			timer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		public function onTimer(event:TimerEvent):void {
			this.bitmapData = currentImgList[currentIndex];
			if(currentIndex<currentImgList.length-1) {
				currentIndex++;
			}
			else {
				currentIndex = 0;
				dispatchEvent(new Event("animationEnd"));
			}
		}
		
		public function play():void {
			this.bitmapData = currentImgList[0];
			currentIndex = 0;
			timer.reset();
			timer.start();
		}
		
		public function stop():void {
			timer.stop();
		}
		/**
		 * 设置动画帧率
		 */ 
		public function set frameRate(rate:int):void {
			this.timer.delay = rate;
		}
		/**
		 * 设置动画当前运动序列
		 */ 
		public function set frame(obj:Object):void {
			currentImgList = _frames[obj];
		}
		
		/**
		 * 设置动画所有运动序列
		 * @param frame：帧序列名
		 * @param imgList：帧序列所有帧 
		 */ 
		public function setframe(frame:Object,imgList:Vector.<BitmapData>):void {
			_frames[frame] = imgList;
		}
	}
}